﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALAds : DALBase
    {
        public static DALAds Singleton = new DALAds();

        public GTAd Add(GTAd gtAd)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newAd = new Ad();

                newAd.CategoryId = gtAd.CategoryId;
                newAd.CustomerId = gtAd.CategoryId;
                newAd.Description = gtAd.Description;
                newAd.Email = gtAd.Email;
                newAd.Name = gtAd.Name;
                newAd.Site = gtAd.Site;
                newAd.Skype = gtAd.Skype;
                newAd.Stamp = DateTime.Now;
                newAd.Status = (byte) AdStatusEnum.New;
                newAd.UpdateStamp = DateTime.Now;
                newAd.telephone1 = gtAd.telephone1;
                newAd.telephone2 = gtAd.telephone2;
                newAd.telephone3 = gtAd.telephone3;
                newAd.Rating = gtAd.Rating;

                db.Ads.InsertOnSubmit(newAd);
                db.SubmitChanges();

                gtAd.Id = newAd.Id;
                return gtAd;
            }
        }

        public List<GTAd> TopGet()
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                List<AdsGetTopResult> adsGetTopResults = db.AdsGetTop().ToList();
                return adsGetTopResults.Select(adsGetTopResult => Get(adsGetTopResult.Id)).ToList();
            }
        }


        public GTAd Update(GTAd gtAd)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var newAd = db.Ads.SingleOrDefault(x => x.Id == gtAd.Id);

                newAd.CategoryId = gtAd.CategoryId;
                newAd.CustomerId = gtAd.CategoryId;
                newAd.Description = gtAd.Description;
                newAd.Email = gtAd.Email;
                newAd.Name = gtAd.Name;
                newAd.Site = gtAd.Site;
                newAd.Skype = gtAd.Skype;
                newAd.Status = gtAd.Status;
                newAd.UpdateStamp = DateTime.Now;
                newAd.telephone1 = gtAd.telephone1;
                newAd.telephone2 = gtAd.telephone2;
                newAd.telephone3 = gtAd.telephone3;
                newAd.Rating = gtAd.Rating;

                db.SubmitChanges();
                return gtAd;
            }
        }


        public GTAd Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.Ads.SingleOrDefault(x => x.Id == id);
                GTAd gtBrand = new GTAd();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTAd> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Ads
                 orderby p.Id
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTAd>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTAd gtBrand = new GTAd();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public List<GTAd> GetPaged(int categoryId, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Ads
                 where p.CategoryId == categoryId
                 orderby p.Id
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTAd>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTAd gtBrand = new GTAd();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTAd> GetPaged(AdStatusEnum adStatusEnum, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Ads
                 where p.Status == (decimal) adStatusEnum
                 orderby p.Id
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTAd>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTAd gtBrand = new GTAd();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }



        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.Ads.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.Ads.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }
        
    }
}
